// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import PoolUtil from "../resources/PPSDK/common/Utils/PoolUtil";
import { nodePool } from "./common/miniGame_config";
import { miniGame_data } from "./common/miniGame_data";

const {ccclass, property} = cc._decorator;

@ccclass
export default class miniGame_pig extends cc.Component {

     //默认的速度
     speedY:number=1000;
     game=null;
     protected onEnable(): void {
         this.node.on(cc.Node.EventType.TOUCH_END,this.touchEnd,this);
     }
     protected onDisable(): void {
         this.node.off(cc.Node.EventType.TOUCH_END,this.touchEnd,this);
     }
     initData(){
         this.node.getComponent(sp.Skeleton).setAnimation(0,"wait",true);
         this.node.getComponent(cc.RigidBody).linearVelocity=cc.v2(0,0);
         this.isTouch=false;
     }
     //判断是否已经点击了
     isTouch:boolean=false;
     startPos=null;
     touchEnd(){
         if (miniGame_data.isTurn) {
             miniGame_data.isTurn = false;
             this.node.angle += 180;
             return
         }
         if (miniGame_data.isVanish) {
             miniGame_data.isVanish = false;
             this.game.success();
             PoolUtil.put(nodePool.Pig, this.node);
             return
         }
         if(this.isTouch)return;
         this.isTouch=true;
         this.startPos=this.node.position;
         this.node.getComponent(sp.Skeleton).setAnimation(0,"run",true);
         //点击猪仔 计算某个朝向的运动速度
         let angle = this.node.angle
         let radian= Math.PI / 180 *angle;
         //注意：合成基于 X正方向的方向向量
         let dir = cc.v2(Math.cos(radian),Math.sin(radian));
         let normal=dir.normalizeSelf();
         //计算移动的线速度
         this.node.getComponent(cc.RigidBody).linearVelocity=cc.v2(normal.x*this.speedY,normal.y*this.speedY);
     }
     protected update(dt: number): void {
         if(this.isTouch){
             //判断是否出界面了
             if(this.node.x+50>cc.winSize.width){
                 //回收节点
                 this.game.success();
                 PoolUtil.put(nodePool.Pig,this.node);
             }
         }
     }
     onPreSolve(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider:  cc.PhysicsCollider){
         // 每次将要处理碰撞体接触逻辑时被调用
         if(selfCollider.tag==1&&otherCollider.tag==0&&this.isTouch){
             this.isTouch=false;
             this.game.reCallArr.push({node:this.node,pos:this.startPos})
             this.node.getComponent(cc.RigidBody).linearVelocity=cc.v2(0,0);
             this.node.getComponent(sp.Skeleton).setAnimation(0,"impact",false);
             this.node.getComponent(sp.Skeleton).setCompleteListener((trackEntry, loopCount) => {
                 let name = trackEntry.animation ? trackEntry.animation.name : '';
                 if (name === "impact") {
                     this.node.getComponent(sp.Skeleton).setAnimation(0,"wait",true);
                 }})
         }
     }
}
